package com.demo.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import com.demo.bean.Paper;
import com.demo.bean.ResponseResult;
import com.demo.service.PaperService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/paper")
public class PaperController {

	@Autowired
	PaperService paperService;

	@Value("${site.upload-dir}")
	String uploadDir;

	//@ModelAttribute("paper")
	public Paper initPaper(Integer id) {
		Paper paper = new Paper();
		if (id != null) {
			paper = paperService.get(id);
		}
		return paper;

	}

	/**
	 * 当前create请求,处理两种情况:新增or编辑
	 * 
	 * @return
	 */
	@GetMapping("create")
	public String create(@ModelAttribute("paper") Paper paper) {
		return "paper_create";
	}

	/**
	 * 同时处理新增and编辑
	 * 
	 * @param paper
	 * @return
	 */
	@PostMapping("save")
	public String save(Paper paper) {
		if (paper.getId() == null)
			paperService.save(paper);
		else
			paperService.update(paper);
		return "redirect:list";
	}

	@GetMapping("delete")
	public String delete(Integer id) {
		if (id != null)
			paperService.delete(id);
		return "redirect:list";
	}

	@PostMapping("save2")
	public @ResponseBody ResponseResult<Object> save2(Paper paper) {
		ResponseResult<Object> resp = null;
		int nums = paperService.save(paper);
		if (nums > 0) {
			resp = new ResponseResult<Object>(200, "ok", nums);
		} else {
			resp = new ResponseResult<Object>(500, "异常发生", null);
		}
		return resp;
	}

	@PostMapping("save3")
	public @ResponseBody String save3(@RequestBody Paper paper) {
		int nums = paperService.save(paper);
		String message = nums > 0 ? "ok1" : "fail";
		return message;
	}

	@GetMapping("upload_page")
	public String uploadPage() {
		
		return "paper_create";
	}
	
	@PostMapping("upload")
	public @ResponseBody ResponseResult upload(MultipartFile file, HttpServletRequest request, Model model) 
			throws MultipartException, IOException {
		File newFile = null;
		if(file!=null) {
			String path = request.getServletContext().getRealPath("/");
			File upimg = new File(path + uploadDir);
			if (!upimg.exists())
				upimg.mkdirs();
	
			String newFilePath = upimg.getAbsolutePath() + "\\" + UUID.randomUUID().toString() + ".png";
			newFile = new File(newFilePath);
			file.transferTo(newFile);System.out.println(newFile.getAbsolutePath());
		}
		return new ResponseResult<>(200, "ok" , "/" + uploadDir + "/" + newFile.getName());
	}

	@GetMapping("list")
	public ModelAndView list(@RequestParam(defaultValue = "1", name = "p") Integer currentPage, Model model) {
		PageHelper.startPage(currentPage, 3);
		ModelAndView mv = new ModelAndView("paper_list");
		List<Paper> papers = paperService.list();
		model.addAttribute("p", new PageInfo<Paper>(papers));
		System.out.println(new PageInfo<Paper>(papers));
		model.addAttribute("papers", papers);
		return mv;
	}
}
